import pytest
from common.request_util_test import RequestUtil
from common.test_yaml_util_test import YamlUtil
from config import datas


class Testcronjob:
    url_unify = "/clusters/host"

    @pytest.mark.normal
    @pytest.mark.k8sresource
    @pytest.mark.cronjob
    @pytest.mark.parametrize('cronjobinfo',
                             YamlUtil().read_testcase_yaml('v25.06/resourcemanage/cronjob/1_get_cronjob_list.yaml'))
    def test_get_cronjob_list(self, cronjobinfo):
        # 获取cronjob列表接口-正常
        base_url = datas.baseUrl + Testcronjob.url_unify
        url = base_url + cronjobinfo['request']['url'].replace('{namespace}', 'ingress-nginx')
        method = cronjobinfo['request']['method']
        req = RequestUtil().send_request(method, url)
        assert req.status_code == cronjobinfo['validate'][0]['equals']['status_code']

    @pytest.mark.abnormal
    @pytest.mark.k8sresource
    @pytest.mark.cronjob
    @pytest.mark.parametrize('cronjobinfo', YamlUtil().read_testcase_yaml(
        'v25.06/resourcemanage/cronjob/1_get_cronjob_list_exception.yaml'))
    def test_get_cronjob_list_exception(self, cronjobinfo):
        # 获取cronjob列表接口-异常
        base_url = datas.baseUrl + Testcronjob.url_unify
        url = base_url + cronjobinfo['request']['url'].replace('{namespace}', 'ingress-nginx')
        method = cronjobinfo['request']['method']
        req = RequestUtil().send_request(method, url)
        assert req.status_code == cronjobinfo['validate'][0]['equals']['status_code']

    @pytest.mark.normal
    @pytest.mark.k8sresource
    @pytest.mark.cronjob
    @pytest.mark.parametrize('cronjobinfo',
                             YamlUtil().read_testcase_yaml('v25.06/resourcemanage/cronjob/2_create_cronjob.yaml'))
    def test_create_cronjob(self, cronjobinfo):
        # 创建cronjob接口-正常
        base_url = datas.baseUrl + Testcronjob.url_unify
        url = base_url + cronjobinfo['request']['url'].replace('{namespace}', 'default')
        method = cronjobinfo['request']['method']
        data = cronjobinfo['request']['data']
        req = RequestUtil().send_request(method, url, json=data)
        cronjobname = req.json()['metadata']['name']
        globals()['cronjobname'] = cronjobname
        assert req.status_code == cronjobinfo['validate'][0]['equals']['status_code']

    @pytest.mark.abnormal
    @pytest.mark.k8sresource
    @pytest.mark.cronjob
    @pytest.mark.parametrize('cronjobinfo', YamlUtil().read_testcase_yaml(
        'v25.06/resourcemanage/cronjob/2_create_cronjob_exception.yaml'))
    def test_create_cronjob_exception(self, cronjobinfo):
        # 创建cronjob接口-异常
        base_url = datas.baseUrl + Testcronjob.url_unify
        url = base_url + cronjobinfo['request']['url'].replace('{namespace}', 'default')
        method = cronjobinfo['request']['method']
        data = cronjobinfo['request']['data']
        req = RequestUtil().send_request(method, url, json=data)
        assert req.status_code == cronjobinfo['validate'][0]['equals']['status_code']

    @pytest.mark.normal
    @pytest.mark.k8sresource
    @pytest.mark.cronjob
    @pytest.mark.parametrize('cronjobinfo',
                             YamlUtil().read_testcase_yaml('v25.06/resourcemanage/cronjob/3_get_specific_cronjob.yaml'))
    def test_get_specific_cronjob(self, cronjobinfo):
        # 获取指定cronjob接口-正常
        base_url = datas.baseUrl + Testcronjob.url_unify
        url = (base_url + cronjobinfo['request']['url']
               .replace('{namespace}', 'default').replace('{cronjobname}', globals()['cronjobname']))
        method = cronjobinfo['request']['method']
        req = RequestUtil().send_request(method, url)
        updatereqbody = req.json()
        globals()['updatereqbody'] = updatereqbody
        assert req.status_code == cronjobinfo['validate'][0]['equals']['status_code']

    @pytest.mark.abnormal
    @pytest.mark.k8sresource
    @pytest.mark.cronjob
    @pytest.mark.parametrize('cronjobinfo', YamlUtil().read_testcase_yaml(
        'v25.06/resourcemanage/cronjob/4_update_cronjob_exception.yaml'))
    def test_update_cronjob_exception(self, cronjobinfo):
        # 更新指定cronjob接口-异常
        base_url = datas.baseUrl + Testcronjob.url_unify
        url = (base_url + cronjobinfo['request']['url']
               .replace('{namespace}', 'default').replace('{cronjobname}', globals()['cronjobname']))
        method = cronjobinfo['request']['method']
        data = globals()['updatereqbody']
        req = RequestUtil().send_request(method, url, json=data)
        assert req.status_code == cronjobinfo['validate'][0]['equals']['status_code']

    @pytest.mark.normal
    @pytest.mark.k8sresource
    @pytest.mark.cronjob
    @pytest.mark.parametrize('cronjobinfo', YamlUtil().read_testcase_yaml(
        'v25.06/resourcemanage/cronjob/5_update_section_cronjob.yaml'))
    def test_update_section_cronjob(self, cronjobinfo):
        # 部分更新指定cronjob的接口-正常
        base_url = datas.baseUrl + Testcronjob.url_unify
        url = (base_url + cronjobinfo['request']['url']
               .replace('{namespace}', 'default').replace('{cronjobname}', globals()['cronjobname']))
        method = cronjobinfo['request']['method']
        data = cronjobinfo['request']['data']
        header = cronjobinfo['request']['headers']
        req = RequestUtil().send_request(method, url, headers=header, json=data)
        assert req.status_code == cronjobinfo['validate'][0]['equals']['status_code']

    @pytest.mark.abnormal
    @pytest.mark.k8sresource
    @pytest.mark.cronjob
    @pytest.mark.parametrize('cronjobinfo', YamlUtil().read_testcase_yaml(
        'v25.06/resourcemanage/cronjob/5_update_section_cronjob_exception.yaml'))
    def test_update_section_cronjob_exception(self, cronjobinfo):
        # 部分更新指定cronjob的接口-异常
        base_url = datas.baseUrl + Testcronjob.url_unify
        url = (base_url + cronjobinfo['request']['url']
               .replace('{namespace}', 'default').replace('{cronjobname}', globals()['cronjobname']))
        method = cronjobinfo['request']['method']
        data = cronjobinfo['request']['data']
        header = cronjobinfo['request']['headers']
        req = RequestUtil().send_request(method, url, headers=header, json=data)
        assert req.status_code == cronjobinfo['validate'][0]['equals']['status_code']

    @pytest.mark.normal
    @pytest.mark.k8sresource
    @pytest.mark.cronjob
    @pytest.mark.parametrize('cronjobinfo', YamlUtil().read_testcase_yaml(
        'v25.06/resourcemanage/cronjob/6_delete_specific_cronjob.yaml'))
    def test_delete_specific_cronjob(self, cronjobinfo):
        # 删除cronjob接口-正常
        base_url = datas.baseUrl + Testcronjob.url_unify
        url = (base_url + cronjobinfo['request']['url']
               .replace('{namespace}', 'default').replace('{cronjobname}', globals()['cronjobname']))
        method = cronjobinfo['request']['method']
        req = RequestUtil().send_request(method, url)
        assert req.status_code == cronjobinfo['validate'][0]['equals']['status_code']

    @pytest.mark.abnormal
    @pytest.mark.k8sresource
    @pytest.mark.cronjob
    @pytest.mark.parametrize('cronjobinfo', YamlUtil().read_testcase_yaml(
        'v25.06/resourcemanage/cronjob/6_delete_specific_cronjob_exception.yaml'))
    def test_delete_specific_cronjob_exception(self, cronjobinfo):
        # 删除cronjob接口-异常
        base_url = datas.baseUrl + Testcronjob.url_unify
        url = (base_url + cronjobinfo['request']['url']
               .replace('{namespace}', 'default').replace('{cronjobname}', globals()['cronjobname']))
        method = cronjobinfo['request']['method']
        req = RequestUtil().send_request(method, url)
        assert req.status_code == cronjobinfo['validate'][0]['equals']['status_code']
